﻿<%@ Page Title="用户管理" Language="C#" MasterPageFile="~/master/m.master" %>
<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<link href="../assets/css/plugins/jsTree/style.min.css" rel="stylesheet">
<link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet">
<link href="../assets/css/plugins/iCheck/custom.css" rel="stylesheet">
<style type="text/css">
	.pageControl{
		position: fixed;
		background: #fff;
		right: 0;
		bottom: 0;
		width: calc(100% - 220px);
	}
	.textUser{
		height: 27px;
		font-size: 13px;
		padding: 0 3px;
	}
	.btnsearch{
		font-size: 12px;
		padding: 4px;
	}
	.fa-mars{
		color: #00bfff;
	}
	.fa-venus{
		color: #c7254e;
	}
	@media (min-width: 768px) {
		.modal-dialog {
			width: 768px;
			margin: 30px auto;
		}
	}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" Runat="Server">
	<div class="system-box">
		<div class="left-side ibox-content">
			<div id="using_json"></div>
		</div>
		<div class="right-side">
			<div class="btns-container"></div>
			
			<div class="search-container">
				<div class="input-group">
                	<input type="text" class="form-control textUser" placeholder="在此输入关键字以查找用户">
                	<span class="input-group-btn">
                		<button type="button"  class="btn btn-primary btnsearch">
                			<i class="fa fa-search"></i>&nbsp;搜索
                		</button>
                	</span>
                </div>
			</div>
			
			<div class="right-content-box">
				<div class="ibox-content">
		            <table class="footable table table-stripped table-list">
						<thead></thead>
						<tbody id="pageList"></tbody>
					</table>
				</div>
			</div>
			
			<div class="pageControl"></div>
            
        </div>
	</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" Runat="Server">
	<script src="../assets/js/plugins/jsTree/jstree.min.js"></script>
	<script src="../assets/js/plugins/footable/footable.all.min.js"></script>
	<script src="../assets/js/plugins/validate/jquery.validate.min.js"></script>
	<script src="../assets/js/plugins/validate/messages_zh.min.js"></script>
	<script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
	<script src="../assets/js/page/pageControl.js"></script>
	<script src="../assets/js/page/dept.js"></script>
	<script src="../assets/js/page/rScroll.js"></script>
	<script src="../assets/js/page/tableControl.js"></script>
	
	<script type="application/javascript">	
		$(document).ready(function() {
			//初始化选框
			$(".i-checks").iCheck({
				checkboxClass:"icheckbox_square-green",
				radioClass:"iradio_square-green"
			});
			
			//用于记录当前选中节点id（部门id）
			var deptId = 1;
			//用于记录当前选中节点grade
			var deptGrade = 1;
			//用于给予当前选中节点父id
			var deptParent = 0;
			//用于记录当前选中的用户id
			var uId = 0;
			//用于记录当前页数
			var page = 1;
			//用于记录每页多少条记录
			var size = 10;
		    //模块ID
			var ModuleID = Y.getUrlParam("ModuleID");
			//初始树形列表
			var initTree = true;
			//加载导航列表
			var loadList = function(init) {
				//调用加载层
				layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
				initTree = true;
				Y.API2('Dept', 'GetList', {DeptCode:1}, function(data) {
					//关闭加载层
					layer.closeAll('dialog');
					
					var treeData = {}
					if(!Y.checkNull(data) && data.length > 0){
						//json处理
						data.map(function(item,i){
							treeData.text = item.DeptContent;
							treeData.data = {
								Grade: item.Grade,
								DeptID: item.DeptID,
								ParentID: item.ParentID,
							};
							if(item.Children.length > 0){
								treeData.state = {opened: true};
								treeData.children = deptUtils.getChildNode(item.Children);
							}
						})
						
						//初始化左侧菜单
						$.jstree.destroy ("#using_json");
						$('#using_json').jstree({
							core: {
								data: [treeData]
							}
						}).bind('select_node.jstree', function(event, data) {
							initTree = false;
							//绑定的点击事件
							var node_data = data.node.data;
							deptId = node_data.DeptID;
							deptGrade = node_data.Grade;
							deptParent = node_data.ParentID;
							
							page = 1;
							//加载右侧列表
							loadPageList(deptId,true);
						});
						
						//判断是否为初次调用
						if(!Y.checkNull(init)){
							//获取初始化的相关属性
							deptId = data[0].DeptID;
							deptGrade = data[0].Grade;
							deptParent = data[0].ParentID;
							//加载右侧列表
							loadPageList(deptId,true);
						}
					}
				});
			}
			loadList(true);
			
			//获取右侧内容列表
			var loadPageList = function (deptId, needLayer) {
				if($('.footable thead').children().size() == 0)
					tableControl.initThead2('.footable',['用户名称','用户编码','所属部门','所在岗位','角色','性别','类型','状态']);
		
				pageControl.init('Users','GetList',{
					DeptID: deptId,
					SearchStr: $('.textUser').val(),
					Page: page, 
//					Size: size,
					needLayer: needLayer,
					alwaysCallBack:function(){
						uId = 0;
						$('#editBtn').attr('disabled','disabled');
						$('#deleteBtn').attr('disabled','disabled');
						$('#pageList').empty();
					}
				},function(result){
					page = result.page;
					var tBody = [];
					if (!Y.checkNull(result.data)) {
						if (result.data.Items.length > 0){					
					    	result.data.Items.map(function(item,i){             		
								var tempTr = {
									trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd' ),
									children: [
										{tdText: item.UserID, c:0 , r:0, tdHtml:'', tdId:'', tdClass:'hide'},
										{tdText: (item.UserName || ' '), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
										{tdText: (item.UserNo || ' '), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
										{tdText: (item.DeptName || ' '), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
										{tdText: (item.PostName || ' '), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
										{tdText: (item.RoleName || ' '), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
										{tdText: '', c:0 , r:0, tdHtml:'<i class="fa fa-'+ (item.Sex ? 'mars' : 'venus') +'"></i>', tdId:'', tdClass:''},
										{tdText: (item.UserType == 1 ? '系统账号': '普通账号'), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
										{tdText: (item.UserStatus == 1 ? ' 在职 ' : ' 离职 '), c:0 , r:0, tdHtml: ('<i class="fa fa-'+ (item.UserStatus == 1 ? 'check text-navy' : 'times text-danger') +'"></i>'), tdId:'', tdClass:''},
									]
								};
								tBody.push(tempTr);
							})
					   }
					   rScroll();
					}
					tableControl.initTbody('.footable',(tBody.length > 0 ? tBody : null),7,null);
				});
			}
			
			//选择用户
			$('#pageList').on('click','tr',function(){
				if(uId == parseInt($(this).find('td:first').text()))
					return false;
				uId = parseInt($(this).find('td:first').text());
				$('#pageList tr').removeClass('check');
				$(this).addClass('check');
				if(!isNaN(uId)){
					$('#editBtn').removeAttr('disabled');
					$('#deleteBtn').removeAttr('disabled');
				}
			})
				
		    //点击搜索
			$('.btnsearch').click(function () {
				page = 1;
			    loadPageList(deptId, true);
			});
			
		    //获取按钮
    		commonUtil.loadBtnList(ModuleID,function(){
    			rScroll();
    		});
    		
    		//刷新按钮
    		$('.btns-container').on('click', '#refreshBtn', function () {
    		    loadList();
    		    loadPageList(deptId, false);
    		});

    		//新增按钮
    		$('.btns-container').on('click', '#addBtn', function () {
    			if(initTree){
					swal('请先在目录树选择节点');
					return false;
				}
    		    var addUserTab = $('#addUserTab span', window.parent.document);
    		    addUserTab.parent().attr('href', 'system/AddUser.aspx?deptId=' + deptId);
    		    addUserTab.click();
    		});
    		//编辑按钮
    		$('.btns-container').on('click', '#editBtn', function () {
    			//先关闭已开的编辑窗口
    			commonUtil.closeRepeatWindow('编辑用户');
    			
    		    var eidtUserTab = $('#eidtUserTab span', window.parent.document);
    		    eidtUserTab.parent().attr('href', 'system/EditUser.aspx?id=' + uId);
    		    eidtUserTab.click();
    		});

    		//点击删除按钮
    		commonUtil.initDeleteBtnEvent({
    		    callBack: function () {
    		        Y.API2('Users', 'Remove', { UserID: uId }, function (data) {
    		            layer.closeAll('dialog');
    		            if (data.Success == false) {
    		                swal('删除失败', data.Message, 'error');
    		            } else {
    		                swal('删除成功', '该信息已被你永久删除', 'success');
    		                loadPageList(deptId, true);
    		            }
    		        });
    		    }
    		});
            
    		$('.btns-container').on('click', "#btn-InitPassword", function () {
    		    swal({
    		        title: "您确定要初始化密码吗？",
    		        type: "info",
    		        showCancelButton: true,
    		        confirmButtonText: "确定",
    		        cancelButtonText: "取消",
    		        closeOnConfirm: false
    		    }, function () {
    		        Y.API2('Users', 'InitPassword', { UserID: uId }, function (data) {
    		            layer.closeAll('dialog');
    		            if (data.Success == false) {
    		                swal('初始化失败', data.Message, 'error');
    		            } else {
    		                swal('初始化成功', '该用户密码已经初始化', 'success');
    		                loadPageList(deptId, true);
    		            }
    		        });
    		    });
    		});
		});
	</script>
</asp:Content>